﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LybrixNursingCommon;
using LybrixNursingCRMDesktop.FrameWork;

namespace LybrixNursingCRMDesktop
{
    /// <summary>
    /// Interaction logic for BetAvot.xaml
    /// </summary>
    public partial class BetAvot : Page
    {
        private readonly LybrixNursingCommon.NursingDBContainer _entities;
        string nursingHomeID;
        bool isNew = true;
        NursingHome currentNursingHome;

        public BetAvot()
        {
            InitializeComponent();
            _entities = Repository.Entities;
            nursingHomeID = GetUniqueID();
            BetAvotID.Content = nursingHomeID;
            isNew = true;
        }

        public BetAvot(NursingHome _currentNursingHome)
        {

            InitializeComponent();
            _entities = Repository.Entities;
            currentNursingHome = _currentNursingHome;
            InitCusrrentNursingHome();
            isNew = false;
        }


        private void InitCusrrentNursingHome()
        {
            BetAvotName.Text = currentNursingHome.HomeName;
            ContactName.Text = currentNursingHome.HomeContact;
            ContactPhone.Text = currentNursingHome.HomeContactPhone;
            ContactMobile.Text = currentNursingHome.HomeContactMobile;
            ContactFax.Text = currentNursingHome.meContactFax;
            Address.AppendText(currentNursingHome.HomeAddress);

            BetAvotID.Content = currentNursingHome.HomeID;
           
        }


        private void onSave_Click(object sender, RoutedEventArgs e)
        {
            if (isNew)
            {
                NursingHome newNusringHome = new NursingHome();
                newNusringHome.HomeName = BetAvotName.GetValue();
                newNusringHome.HomeID = nursingHomeID;
                newNusringHome.HomeContact = ContactName.GetValue();
                newNusringHome.HomeContactPhone = ContactPhone.GetValue();
                newNusringHome.HomeContactMobile = ContactMobile.GetValue();
                newNusringHome.meContactFax = ContactFax.GetValue();
                string richText = new TextRange(Address.Document.ContentStart, Address.Document.ContentEnd).Text;
                newNusringHome.HomeAddress = richText;
                _entities.AddToNursingHomes(newNusringHome);
            }
            else
            {
                currentNursingHome.HomeName = BetAvotName.GetValue();
                currentNursingHome.HomeContact = ContactName.GetValue();
                currentNursingHome.HomeContactPhone = ContactPhone.GetValue();
                currentNursingHome.HomeContactMobile = ContactMobile.GetValue();
                currentNursingHome.meContactFax = ContactFax.GetValue();
                string richText = new TextRange(Address.Document.ContentStart, Address.Document.ContentEnd).Text;
                currentNursingHome.HomeAddress = richText;
                
                
            }

            try
            {

                _entities.SaveChanges();
                MessageBox.Show("הרשומה נרשמה בהצלחה", "הודעה", MessageBoxButton.OK);
                NavigationCenter.ShowSearchPage(NavigationTarget.NursingHome, this);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        

        private void ToolBar_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private string GetUniqueID()
        {
            string tmpID = string.Empty;
            int lastvalue = 1;
            string agentID = string.Empty;
            formula _formula;

            var formulas = (from x in _entities.formulae where x.bo == "BetAvotFormula" select x);


            if (formulas.Count<formula>() == 0)
            {
                agentID = lastvalue.ToString("0000");
                _formula = new formula();
                _formula.bo = "BetAvotFormula";
                _formula.lastvalue = 1;
                _entities.AddToformulae(_formula);

            }
            else
            {
                lastvalue = (formulas.First<formula>()).lastvalue;
                _formula = formulas.First<formula>();
                agentID = (lastvalue + 1).ToString("0000");
                _formula.lastvalue = lastvalue + 1;
            }


            try
            {

                _entities.SaveChanges();
                return "BA" + agentID;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return null;
            }




        }

        private void onDelete_Click(object sender, RoutedEventArgs e)
        {
            IQueryable<Teek> teeks = null;
            MessageBoxResult result = MessageBox.Show("האם אתה בטוח שאתה רוצה למחוק רישימה זו", "אזהרה", MessageBoxButton.YesNo);

            if (result == MessageBoxResult.Yes)
            {
                if (currentNursingHome != null)
                {

                    teeks = _entities.Teeks.Where(x => x.NursingHome.Id == currentNursingHome.Id);
                    if (teeks.Count<Teek>() > 0)
                    {
                        MessageBox.Show("לא תוכל למחוק בית אבות זה בגלל שהוא מקושר לרשימות פעילות אחרות.", "אזהרה", MessageBoxButton.OK);
                        return;
                    }
                    else
                    {
                        _entities.DeleteObject(currentNursingHome);
                        _entities.SaveChanges();
                        NavigationCenter.ShowSearchPage(NavigationTarget.NursingHome, this);
                    }
                }
            }
        }
    }
}
